$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'waterfgs/dataerralertfixed/listchart',
        datatype: "json",
        colModel: [
            { label: 'opKey', name: 'opKey', index: 'op_key', width: 50, key: true,hidden:true },
            { label: '加入时间', name: 'opAddDate', index: 'op_add_date', width: 120,align:'center' },
            //{ label: '维修状态', name: 'opFixedFlag', index: 'op_fixed_flag', width: 80 },
            //{ label: '修复时间', name: 'opFixedDate', index: 'op_fixed_date', width: 80 },
            { label: '表卡编号', name: 'clientId', index: 'client_id', width: 60,align:'center' },
            { label: '客户名', name: 'clientName', index: 'client_name', width: 60,align:'center' },
            { label: '客户地址', name: 'clientAddress', index: 'client_address', width: 230 },
            { label: '小区名称', name: 'proName', index: 'pro_name', width: 60 ,align:'center'},
            { label: '设备号', name: 'deviceId', index: 'device_id', width: 60 ,align:'center'},
            { label: '采集日期', name: 'nowDate', index: 'now_date', width: 80,align:'center' },
            { label: '采集数据', name: 'nowData', index: 'now_data', width: 60,align:'center' },
            { label: '设备状态', name: 'deviceStatus', index: 'device_status', width: 60 ,align:'center'},
            //{ label: '之前数据', name: 'agoData', index: 'ago_data', width: 80 },
            //{ label: '之前状态', name: 'agoDeviceStatus', index: 'ago_device_status', width: 80 },

            //{ label: '标识', name: 'delFlag', index: 'del_flag', width: 80 },
            //{ label: '备注1', name: 'memo1', index: 'memo1', width: 80 },
            //{ label: '备注2', name: 'memo2', index: 'memo2', width: 80 },
            //{ label: '备注3', name: 'memo3', index: 'memo3', width: 80 }
        ],
        viewrecords: true,
        height: $(window).height()-165,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 60,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $(function () {
        $('#inputClear').click(function () {
            $('input')[0].value = "";
            vm.q.value="";
        })
    });
});


function zTreeOnDblClick(event, treeId, treeNode) {
    vm.q.dept = treeNode.deptId;
    vm.newpage = true;
    vm.reload();
    //alert(treeNode.deptId);
    //alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
};

var settingLeft = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onDblClick: zTreeOnDblClick
    }
};

var ztree;


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		dataErrFixedInfo: {},
        searchKeyDictInfo: {},
        q:{
            key:null,
            value:null,
            dept:null
        }
	},
	methods: {
        getDeptTreeLeft: function(){
            $.get(baseURL + "water/waterdept/queryDeptErrSubAlertWater", function(r){
                //console.log(r);
                ztree = $.fn.zTree.init($("#deptTreeLeft"), settingLeft, r);
                var node = ztree.getNodeByParam("deptId", vm.q.dept);
                if(node != null){
                    ztree.selectNode(node);
                    vm.q.dept = node.deptId;
                }
                ztree.expandAll(true);
            })
        },
        getSearchKeyDict: function(){
            $.get(baseURL + "sys/dict/list",{name:"qkey_vdataerr"}, function(r){
                vm.searchKeyDictInfo = r.page.list;
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dataErrFixedInfo = {};
		},
		update: function (event) {
			var opKey = getSelectedRow();
			if(opKey == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(opKey)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.dataErrFixedInfo.opKey == null ? "waterfgs/dataerrfixedinfo/save" : "waterfgs/dataerrfixedinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.dataErrFixedInfo),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var opKeys = getSelectedRows();
			if(opKeys == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要将选中的记录设置为：<br>已经更换完毕，设置后将清除记录！', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "waterfgs/dataerrfixedinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(opKeys),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(opKey){
			$.get(baseURL + "waterfgs/dataerrfixedinfo/info/"+opKey, function(r){
                vm.dataErrFixedInfo = r.dataErrFixedInfo;
            });
		},
        outxls:function(){
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
            var url = baseURL
                + "waterfgs/dataerralertfixed/export?params="
                + encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:"-1",page:"1"}));

            //console.log(decodeURIComponent(url));
            location.href = url;
        },
		reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            if(vm.newpage){page = "1";}
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		}
	},
    created: function (){
        this.getDeptTreeLeft();
        this.getSearchKeyDict();
    }
});